#!/usr/bin/perl

open (GP, "|tee plotlog.txt| gnuplot") or die "no gnuplot";
# force buffer to flush after each write  
use FileHandle;  
GP->autoflush(1);

print GP "set term epslatex standalone color size 10cm,12cm;";
print GP "set output 'converge.tex';";

my @files=("O18.txt","O18_exp.txt");
my @colss=([10,4,5,3],[3]);
my @xticss=(["bare","2nd","3rd","IM-SRG"],["Expt."]);
my @label_col=(13,5);
# my @files=("O18.txt");
# my @colss=([4,5,3]);
# my @xticss=(["2nd","3rd","IM-SRG"]);
$script=genPlotStr(\@files,\@colss,\@xticss,\@label_col);
print GP $script;



sub genPlotStr(@files,@colss,@xticss,@label_col){
    ($files,$colss,$xticss,$label_col)=@_;
    $nfiles=scalar @files;
    $totalcols=0;
    $set_str="";
    for($i=0;$i<$nfiles;$i++)
    {
    	$cols=$colss[$i];
	$ncol=scalar @$cols;
	$totalcols+=$ncol;
    }
#    print $totalcols;
    $left=0.25;#left margin
    $dx=1;
    $bw=0.5;
    $right=0.55;
    $lw=0.08;
    $dd=0.2;
    #         dd
    #         /\
    #left|----  label ----  ----|right
    #     \__________/\__/   
    #       dx         bw
    #head setting
    $xrange=$left+$dx*($totalcols-1)+$bw+$right;
    $set_str=$set_str."set xrange [0:$xrange];unset key;set boxwidth $bw;unset xtics;";
#    $set_str=$set_str."set yrange [-0.5:7.2];";
#    $set_str=$set_str."set label '\\scalebox{1.2}{\$^{18}\\mathrm{O}\$}' at $left+0.5*$bw,6;";

    # $set_str=$set_str."set linetype 1 lc rgb '#43CD80';";#00CED1';";#7c00c4
    # $set_str=$set_str."set linetype 2 lc rgb '#CD3278';";#BC8F8F';";#66CD00';";#2a9060
    # $set_str=$set_str."set linetype 3 lc rgb '#CD9B1D';";#ec1828';";#d3000f    
    # $set_str=$set_str."set linetype 4 lc rgb '#5F9EA0';";#3334ff';";# 285ea2
    # $set_str=$set_str."set linetype 5 lc rgb '#838B83';";#black';";##434343


    $set_str=$set_str."set linetype 1 lc rgb '#7c00c4';";
    $set_str=$set_str."set linetype 2 lc rgb '#2a9060';";
    $set_str=$set_str."set linetype 3 lc rgb '#ec1828';";#d3000f    
    $set_str=$set_str."set linetype 4 lc rgb '#3334ff';";# 285ea2
    $set_str=$set_str."set linetype 5 lc rgb 'black';";##434343

    
    $startcol=0;
    $plot='plot ';
    $xticsString="";    
    for($j=0;$j<$nfiles;$j++)
    {
	$file=$files[$j];
    	$cols=$colss[$j];
    	$xtics=$xticss[$j];
	$ncol=scalar @$cols;
#	print $j,$file."\n";
        open($fh,$file);
	@shifts=();
	@heads=();
	$count=0;
	$labelString="";
	$dashline="";
	while($line=<$fh>)
	{
	    @array=split/\s+/,$line;
	    for($i=0;$i<$ncol;$i++)
	    {
		$x=$left+$dx*($i+$startcol)+$bw/2;
		$col=@$cols[$i];
		#set head
		if($count==0)
		{
		    $heads[$i]=$array[$col-1];
		    $xticsString=$xticsString . "'@$xtics[$i]' $x,";
		}
		if($count==1)
		{
		    $shifts[$i]=$array[$col-1];
		    #	    $shifts[$i]=0;	    
		}
		#set label
		if($count!=0)
		{
		    $labelx=$x+0.5*$bw+$dd;
		    if($i==$ncol-1)
		    {
			$labelString=$labelString."set label '\\scalebox{0.6}{\$$array[0]^$array[1]\$}' at $labelx,$array[$label_col[$j]-1]-$shifts[$i];";
			$y=$array[$col-1]-$shifts[$i]+0.5*$lw;
			$dashline=$dashline."set arrow from ($x+0.5*$bw),$y to $labelx,$array[$label_col[$j]-1]-$shifts[$i] nohead dt (2,2) lt 0 lw 3;";			
		    }
		    if($i>0)
		    {
			$oldx=$left+$dx*($i-1+$startcol)+$bw/2;
			$oldcol=@$cols[$i-1];
			$oldy=$array[$oldcol-1]-$shifts[$i-1]+0.5*$lw;
			$y=$array[$col-1]-$shifts[$i]+0.5*$lw;
			$dashline=$dashline."set arrow from ($oldx+0.5*$bw),$oldy to ($x-0.5*$bw),$y nohead dt (2,2) lt 0 lw 3;";
		    }
		}
	    }
	    $count=$count+1;
	}
	print GP $labelString;
	print GP $dashline;
	#print $xticsString;
	#	print $labelString;
	for($i=0;$i<$ncol;$i++)
	{
	    $x=$left+$dx*($i+$startcol)+$bw/2;
	    $col=@$cols[$i];
	    $color=$i+$startcol+1;
	    $plot=$plot."\'$file\' using ($x):(\$$col+$lw-$shifts[$i]):(\$$col+$lw-$shifts[$i]):(\$$col-$shifts[$i]):(\$$col-$shifts[$i]):($color) linecolor variable with candlesticks,";
	    #:($color) linecolor variable
	}
	$startcol=$startcol+$ncol;
	close($fh);
    }
    $set_str=$set_str."set xtics (".$xticsString.");";    
    $plot=$set_str.$plot."\n";
#    print $plot;
    return $plot;
}
